from datetime import datetime

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH, DeviceInfoHelper
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting, DeskTop


class PreTest_RK3568_Basic_Setting_4300(TestCase, Setting):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1.进入桌面，点击设置-系统-日期和时间-日期
    # 2.上下滑动修改“年月日”后点击确定
    # 预期结果：
    # 1.可正常进入日期界面
    # 2.“年月日”上下滑动正常，日期修改生效

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：进入桌面，点击设置-系统-日期和时间-日期')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 3)
        CommonOH.touchByText(self.Phone1, '系统')
        CommonOH.touchByText(self.Phone1, '日期和时间')
        current_date = CommonOH.getTextByCondition(self.Phone1, condition='日期', relativePath='AFTER', index=1)
        CommonOH.touchByText(self.Phone1, '日期')
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果1：可正常进入地区')
        now = datetime.now()
        CommonOH.checkIfTextExist(self.Phone1, '{}年'.format(now.year))
        CommonOH.checkIfTextExist(self.Phone1, '{}月'.format(now.month))
        CommonOH.checkIfTextExist(self.Phone1, '{}日'.format(now.day))

        Step('步骤2：上下滑动修改“年月日”后点击确定')
        # 年
        CommonOH.swipe(self.Phone1, x1=146, y1=1018, x2=146, y2=854, steps=200)
        # 月
        CommonOH.swipe(self.Phone1, x1=333, y1=854, x2=333, y2=1018, steps=200)
        # 日
        CommonOH.swipe(self.Phone1, x1=524, y1=1018, x2=524, y2=854, steps=200)
        CommonOH.touchByText(self.Phone1, '确定')
        CommonOH.wait(self.Phone1, 1)
        Step('预期结果2: “年月日”上下滑动正常，日期修改生效')
        new_date = CommonOH.getTextByCondition(self.Phone1, condition='日期', relativePath='AFTER', index=1)
        Asserts.assert_not_equal(new_date, current_date)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
